﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Models;
using GDGeek;
using System;

public class EnemyInBattle : MonoBehaviour {


    public const string EnemyDead = "敌人死亡状态";
    public const string System_DoAction = "系统自动轮询执行策略状态";
    public const string EnemyWait = "敌人等待状态";

    //状态转变发送的key
    public const string EnemyWait_ChangeTo_System_DoAction = "敌人等待状态z转换为z系统自动轮询执行策略状态";
  
    public const string System_DoAction_ChangeTo_EnemyDead = "系统自动轮询执行策略状态z转换为z敌人死亡状态";

    public const string System_DoAction_ChangeTo_EnemyWait = "系统自动轮询执行策略状态z转换为z敌人等待状态";


    private SpriteRenderer e_render;

    public EnemyBase enemy;

    public string enemy_fire_prefab;

    public EnemyState state_enemy;

    public FSM fsm_enemy = new FSM();

    //public int hp;
	// Use this for initialization
	void Start () {
        state_enemy = EnemyState.Alive;

        e_render = GetComponent<SpriteRenderer>();
        InitFSM();
        StartCoroutine(ToEnemySystemDoAction());

        HUDRoot = GameObject.Find("HUDText").GetComponent<bl_HUDText>();
    }
    public void CoroutineToEnemySystemDoAction()
    {
        StartCoroutine(ToEnemySystemDoAction());
    }

    IEnumerator ToEnemySystemDoAction()
    {
        yield return new WaitForSeconds(0.5f);
        FSMEnemyWait_ChangeTo_System_DoAction();
    }

    void InitFSM()
    {
        fsm_enemy.addState(EnemyDead, OnEnemyDead());
        fsm_enemy.addState(System_DoAction , OnSystem_DoAction());
        fsm_enemy.addState(EnemyWait, OnEnemyWait());

        fsm_enemy.init(EnemyWait);
    }

    private State OnEnemyWait()
    {
        StateWithEventMap state = new StateWithEventMap();


        state.onStart += delegate
        {
            AddActackPlayerAction();
            state_enemy = EnemyState.Alive;
            // FSM_System_Wait_ChangeTo_PlayerOneOperation();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };
        state.addAction(EnemyWait_ChangeTo_System_DoAction , System_DoAction);


        return state;
    }

    private State OnSystem_DoAction()
    {
        StateWithEventMap state = new StateWithEventMap();


        state.onStart += delegate
        {

            state_enemy = EnemyState.Alive;
            // FSM_System_Wait_ChangeTo_PlayerOneOperation();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };
        state.addAction(System_DoAction_ChangeTo_EnemyDead, EnemyDead);
        state.addAction(System_DoAction_ChangeTo_EnemyWait, EnemyWait );


        return state;
    }

    public void FSMSystem_DoAction_ChangeTo_EnemyWait()
    {
        fsm_enemy.post(System_DoAction_ChangeTo_EnemyWait);
    }

    public void FSMSystem_DoAction_ChangeTo_EnemyDead()
    {
        fsm_enemy.post(System_DoAction_ChangeTo_EnemyDead);
    }

    public void FSMEnemyWait_ChangeTo_System_DoAction()
    {
        fsm_enemy.post(EnemyWait_ChangeTo_System_DoAction);
    }

    private State OnEnemyDead()
    {
        StateWithEventMap state = new StateWithEventMap();


        state.onStart += delegate
        {

            state_enemy = EnemyState.Dead;
            // FSM_System_Wait_ChangeTo_PlayerOneOperation();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };
      //  state.addAction(System_Wait_ChangeTo_PlayerOneOperation, PlayerOneOperation);


        return state;
    }

    public bl_HUDText HUDRoot;

    public void  GetDamage( int hitPoint)
    {
        enemy.NowHp  -= hitPoint;

        //HUDRoot.NewText("- " + hitPoint.ToString(), base.transform, Color.red, 8, 20f, -1f, 2.2f, bl_Guidance.RightDown);

        HUDRoot.NewText("- " + hitPoint.ToString(), base.transform, Color.red, 20 , 20f, -1f, 2.2f, bl_Guidance.Up);

        if (enemy.NowHp <= 0)
        {
            HUDRoot.NewText("-"+ hitPoint +" "+ enemy.Name +"被消灭", base.transform, Color.red, 20, 20f, -1f, 2.2f, bl_Guidance.Up);

            FSMSystem_DoAction_ChangeTo_EnemyDead();
            state_enemy = EnemyState.Dead;

            SetThisEnemyOutScreen();

            DestoryEnemySelectButton();
        }
    }

    void SetThisEnemyOutScreen()
    {
        //CommonHelper.SetEnemyOutOfScreen(this.gameObject);
        StartCoroutine(WaitSetEnemyOutOfScreen());
    }

    IEnumerator WaitSetEnemyOutOfScreen()
    {
        yield return new WaitForSeconds(0.5f);
        CommonHelper.SetEnemyOutOfScreen(this.gameObject);
    }

    void DestoryEnemySelectButton()
    {
        GameObject[] selectEnemyButtonArray = GameObject.FindGameObjectsWithTag("SelectEnemyButton");
        for(int i = 0; i < selectEnemyButtonArray.Length; i++)
        {
            GameObject e = selectEnemyButtonArray[i].GetComponent<EnemySelectButtonFlag>().GetButtonPoint_Enemyobject();
            if(e == this.gameObject)
            {
                Destroy(selectEnemyButtonArray[i]);
            }
        }



    }

    public void SetEnemyDead()
    {
        state_enemy = EnemyState.Dead;
    }

    public bool isDead()
    {
        if (state_enemy == EnemyState.Dead)
            return true;
        else
            return false;
    }



    // Update is called once per frame
    void Update ()
    {
		  if(state_enemy == EnemyState.Dead)
        {
            e_render.enabled = false;
        }
	}

    public void AddActackPlayerAction()
    {
        //添加攻击的 DoAction 到 Action列表里面
        BattleAction action = new BattleAction();
        action.targetPos = GameObject.Find("Player11").transform.position  ;
        action.actiontype = BattleActionType.BaseEnemyAttack ;
        action.next_trun_speed = 3; // 当前这个敌人的攻击trun先后顺序 的速度
        action.owner = this.gameObject;   //当前选择操作的玩家
        action.person_long_range_attack_fireName = enemy_fire_prefab;// "Fire_Enemy";
        action.next_trun_speed = 5;

        TurnSystem.Instance.AddAction(action);
    }



}

public enum EnemyState
{
     Alive,
     Dead
}

[System.Serializable]
public class EnemyBase
{
    public string Name;
    public int NowHp;
    public int MaxHp;

    public int Attack;
    public int FangYu;

    public int GiveExp;

    public int GiveGold;
}
